package com.mpsc.spring.demo.controller;

import com.mpsc.api.unit.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

@RestController
@RequestMapping("/file")
@Api(value = "FileController", tags = "文件管理")
public class FileController {

    @GetMapping("/create")
    @ApiOperation("创建一个文件")
    public void create() {
        FileUtil.createFile("D:\\kdxf\\1.txt");
    }

    @GetMapping("/write")
    @ApiOperation("循环向文件中写内容")
    public void write() {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream("D:\\kdxf\\1.txt", true)));
            for (int i = 0; i < 10; i++) {
                out.write(i + "\r\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @GetMapping("/write2")
    @ApiOperation("循环向文件中写内容")
    public void write2() {
        for (int i = 0; i < 10; i++) {
            FileUtil.writeFileContent("D:\\kdxf\\1.txt", i + "aa");
        }
    }


}
